<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Testdb extends CI_Model
{
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    /***************************************************************************
     * Save Histogram
     * Saves a histogram into the database. The parameter expected must be in the
     * following format:
     * 
     * array(
     *   'img' => string(path to image file being indexed)
     *   'bucket_1' .. 'bucket_23' => string(frequency for given bucket)
     *   'channel' => string(name of table where data is to be saved)
     * )
     **************************************************************************/
    function saveHisto($pData)
    {
       $this->load->database();
       
       // Sadly, code Igniter doesn't ignore columns not in the table. Thus,
       // we must store this value from pData and remove it from the array.
       $table = $pData['table_name'];
       unset($pData['table_name']);
       
       return $this->db->insert($table, $pData);
    }
    
    
    
    private function prepareQueryStringDiffBuckets(array $pData, $pCol)
    {
       $str = '';
       
       foreach($pData as $key => $val)
          $str .= "ABS({$pCol}.{$key} - {$val}) + ";
       
       $str .= 0;
       
       return $str;
    }
    
    
    function matchit(array $pData)
    {
       $this->load->database();
       
       $query = "SELECT RED.img, 
               ABS(
                  ABS(". $this->prepareQueryStringDiffBuckets( $pData['red'],   'RED'   ). ") -
                  ABS(". $this->prepareQueryStringDiffBuckets( $pData['green'], 'GREEN' ). ") -
                  ABS(". $this->prepareQueryStringDiffBuckets( $pData['blue'],  'BLUE'  ). ")
               )TOTAL
                  
               FROM histogram_red_23 RED
               JOIN histogram_green_23 GREEN ON GREEN.img = RED.img
               JOIN histogram_blue_23 BLUE ON BLUE.img = GREEN.img
               
               WHERE RED.img <> '{$pData['img']}'
               ORDER BY TOTAL
               ";
               
       $res = $this->db->query($query);
       
       return $res;
    }
    
    
    function formatResultsHTML($pData)
    {
       $html = '';
       
       foreach($pData->result() as $row)
       {
          $total = (int)($row->TOTAL / 23 / 1000);
          $html .= "<div style='display:block;width:25%;float:left;padding:0;margin:0;'>
          <img src='{$row->img}' title='{$row->TOTAL}' style='display:block;width:100%'/>
          <p style='margin: -19px 0 0; padding: 0;background: #333; color: #fff;position:relative;width:100%'>Score: {$total}</p>
          </div>
          ";
       }
       
       return $html;
    }
    
    function getFake()
    {
       return 'data from fake function in TestDb Model';
    }
    
    function fakeInsert($pData)
    {
       $this->load->database();
       
       $table = $pData['channel'];
       unset($pData['channel']);
       
       return $this->db->insert($table, $pData);
    }

}

/* End of file testdb.php */
/* Location: ./application/models/testdb.php */